package refletion;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * @program: thread
 * @description:
 * @author: gongj
 * @Description: TODO
 * @create: 2020-06-04 22:33
 **/
//测试反射的方法
public class Test6 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException {
        Class<?> user = Class.forName("refletion.User");

        //获得类的名字
        System.out.println("获得类的名字");
        String name = user.getName();
        System.out.println(name);
        String simpleName = user.getSimpleName();
        System.out.println(simpleName);
        System.out.println("获得本类以及父类的public字段");

        //获得本类的字段  获得public的属性
        Field[] fields = user.getFields();
        for (Field field : fields) {
            System.out.println(field);
        }

        System.out.println("获得本类的所有字段");
        Field[] declaredFields = user.getDeclaredFields();
        for (Field declaredField : declaredFields) {
            System.out.println(declaredField);

        }
        System.out.println("获得类的public的指定字段");
        Field id = user.getField("id");
        System.out.println(id);

        System.out.println("获得类的指定字段");
        Field name1 = user.getDeclaredField("name");
        System.out.println(name1);

        System.out.println("获得本类以及父类公开的方法");
        Method[] methods = user.getMethods();
        for (Method method : methods) {
            System.out.println("公开的方法" + method);
        }
        System.out.println("获得本类所有的方法");
        Method[] getDeclaredMethods = user.getDeclaredMethods();
        for (Method method : getDeclaredMethods) {
            System.out.println("所有的方法" + method);
        }

    }
}
